package com.example.casper.Experiment2024.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.casper.Experiment2024.data.Bomb;
import com.example.casper.Experiment2024.data.Submarine;
import java.util.ArrayList;
import java.util.List;

public class GameView extends SurfaceView implements Runnable {
    private long nowtime;
    private Thread gameThread;
    private boolean isPlaying;
    private SurfaceHolder surfaceHolder;
    private Paint paint;
    private int screenWidth, screenHeight;
    private Submarine submarine;
    private List<Bomb> bombs;
    private long startTime, currentTime;
    private int dodgedBombs;
    private boolean isGameOver;
    public GameView(Context context) {
        super(context);
        init();
    }
    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    private void init() {
        surfaceHolder = getHolder();
        paint = new Paint();
        bombs = new ArrayList<>();
        dodgedBombs = 0;
        isGameOver = false;
    }
    @Override
    public void run() {
        startTime = System.currentTimeMillis();
        while (isPlaying) {
            update();
            draw();
            control();
        }
    }
    private void update() {
        if (isGameOver) return;

        // 更新潜艇位置
        submarine.update();

        // 更新炸弹位置
        for (Bomb bomb : bombs) {
            bomb.update();
            if (bomb.y > screenHeight) {
                dodgedBombs++;
                bomb.resetPosition();
            }
            // 检测碰撞
            if (bomb.isColliding(submarine)) {
                isGameOver = true;
            }
        }
        // 每隔一定时间增加炸弹
        if ((System.currentTimeMillis() - startTime) > 5000 && bombs.size() < 10) {
            bombs.add(new Bomb(screenWidth, screenHeight));
        }
    }
    private void draw() {

        if (!surfaceHolder.getSurface().isValid()) return;

        Canvas canvas = surfaceHolder.lockCanvas();
        canvas.drawColor(Color.WHITE);

        submarine.draw(canvas, paint);

        for (Bomb bomb : bombs) {
            bomb.draw(canvas, paint);
        }

        if (isGameOver) {
            paint.setTextSize(100);
            canvas.drawText("Game Over", screenWidth / 2f - 200, screenHeight / 2f, paint);
            canvas.drawText("Time: " + nowtime + "s", 50, 200, paint);
        }else {
            nowtime=currentTime / 1000;
            canvas.drawText("Time: " + nowtime + "s", 50, 200, paint);
        }
        // 绘制分数和时间
        paint.setColor(Color.BLACK);
        paint.setTextSize(50);
        currentTime = System.currentTimeMillis() - startTime;
        canvas.drawText("Dodged: " + dodgedBombs, 50, 100, paint);
        surfaceHolder.unlockCanvasAndPost(canvas);
    }

    private void control() {
        try {
            Thread.sleep(17); // 控制帧率约为60 FPS
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isGameOver && submarine != null) {
            submarine.handleTouchEvent(event);
        }else
        {
            resetGame();
        }
        return true;
    }

    public void pause() {
        isPlaying = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void resume() {
        isPlaying = true;
        gameThread = new Thread(this);
        gameThread.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        screenWidth = w;
        screenHeight = h;

        // 初始化潜艇
        submarine = new Submarine(getContext(), screenWidth, screenHeight);

        // 初始化炸弹
        bombs.clear();
        bombs.add(new Bomb(screenWidth, screenHeight));
    }
    private void resetGame() {
        isGameOver = false;
        dodgedBombs = 0;
        currentTime = 0;
        bombs.clear();
        bombs.add(new Bomb(screenWidth, screenHeight));
        submarine = new Submarine(getContext(), screenWidth, screenHeight);
        startTime = System.currentTimeMillis();
    }
}